package cn.lopr.im.android.utils;

import android.os.Handler;
import android.util.Log;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft_6455;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;


public class IMWebSocketClient {

    private static WebSocketClient client = null;

    private static Handler handler = new Handler();

    public static void connect(final WebSocketListener webSocketListener) {
        Log.e("IMWebSocketClient: ", "connect: ");
        Thread webSocketThread = new Thread() {
            @Override
            public void run() {
                try {
                    WebSocketClient mSocketClient = new WebSocketClient(new URI(Apis.DEPLOY_WS_URL), new Draft_6455()) {
                        @Override
                        public void onOpen(final ServerHandshake handshake) {
                            handler.post(() -> {
                                if (webSocketListener != null) {
                                    webSocketListener.onOpen(handshake);
                                }
                            });
                        }

                        @Override
                        public void onMessage(final String message) {
                            handler.post(() -> {
                                if (webSocketListener != null) {
                                    webSocketListener.onMessage(message);
                                }
                            });
                        }

                        @Override
                        public void onClose(final int code, final String reason, final boolean remote) {
                            handler.post(() -> {
                                if (webSocketListener != null) {
                                    webSocketListener.onClose(code, reason, remote);
                                }
                            });
                        }

                        @Override
                        public void onError(final Exception ex) {
                            handler.post(() -> {
                                if (webSocketListener != null) {
                                    webSocketListener.onError(ex);
                                }
                            });
                        }
                    };

                    client = mSocketClient;
                    mSocketClient.connectBlocking();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };

        webSocketThread.start();
    }


    public static WebSocketClient getWebSocketClient() {
        return client;
    }

    public static void disConnect() {
        client.close();
    }

    public interface WebSocketListener {

        void onOpen(ServerHandshake handshakedata);

        void onClose(int code, String reason, boolean remote);

        void onMessage(String message);

        void onError(Exception ex);
    }
}